#include "enemyplane.h"
#include "config.h"
#include <QMovie>
#include <QObject>

EnemyPlane::EnemyPlane()
{
    //敌机资源加载
    m_enemy.load(ENEMY_PATH);

    //敌机位置
    m_X = GAME_WIDTH;
    m_Y = GAME_HEIGHT;

    //敌机状态
    m_Free = true;

    //敌机速度
    m_Speed = ENEMY_SPEED;

    //敌机矩形
    m_Rect.setWidth(m_enemy.width());
    m_Rect.setHeight(m_enemy.height());
    m_Rect.moveTo(m_X,m_Y);
}

void EnemyPlane::updatePosition()
{
    //空闲状态，不计算坐标
    if(m_Free)
    {
        return;
    }

    m_X -= m_Speed;
    m_Rect.moveTo(m_X,m_Y);

    if(m_X <= 0 - m_Rect.width())
    {
        m_Free = true;
    }
}

void EnemyPlane::initAnimation(const QString& gifPath)
{
    m_movie = new QMovie(gifPath);
    m_movie->setCacheMode(QMovie::CacheAll); // 缓存所有帧提高性能
    m_movie->start();

    // 连接帧变化信号
    connect(m_movie, &QMovie::frameChanged, [this](int){
        m_currentFrame = m_movie->currentPixmap();
    });
}
